package com.groupsix.controller;

import com.groupsix.service.OrderService;
import com.groupsix.vo.ActionResult;
import com.groupsix.vo.OrderInfoVo;
import com.groupsix.vo.OrderListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;

    @RequestMapping("carorder")
    public OrderInfoVo bookOrder(int getid, int backid, int cid){
        System.out.println("getid = " + getid+"backid="+backid+" cid="+cid);
        OrderInfoVo orderInfoVo = orderService.searchOrderInfo(getid, backid, cid);
        return orderInfoVo;
    }

    @RequestMapping("addOrder")
    public String addOrder(int getid, int backid, int cid, HttpSession session){
        session.setAttribute("user.tel","13783078934");
        System.out.println("getid = " + getid);
        String tel = (String)session.getAttribute("user.tel");
        boolean b = orderService.addOrder(getid, backid, cid, tel);
        if(b) {
//        执行写入订单
            return "success";
        }else{
            return "fail";
        }
    }

    @RequestMapping("myOrders")
    public ActionResult myOrders(HttpSession session){
        session.setAttribute("user.id",1);
        ActionResult result = null;
//        获取当前用户id
        Object obj =session.getAttribute("user.id");
        if(ObjectUtils.isEmpty(obj)){
//            当前用户未登录 返回消息，去登陆
            result = new ActionResult(-200,"fail",null);
        }else{//用户已登录，直接查询结果
            int uid = (int )obj;
//            根据用户id查询我的订单信息集合
            List<OrderListVo> orderList = orderService.findMyOrders(uid);
            result = new ActionResult(200,"success",orderList);
        }
        return result;
    }

    @RequestMapping("myOrderDel")
    public String myOrderDel(Integer id){
        System.out.println("id = " + id);
        int delet = orderService.delet(id);
        if(delet !=0){
            return "success";
        }else{
            return "fail";
        }
    }
}
